import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { charts } from "@/api/charts"
import axios from 'axios'


const user = {
  state: {
    token: getToken(),
    name: '',
    nickName: '',
    tagName: '',
    headClass: '',
    avatar: '',
    type: '',
    phonenumber:'',
    qq: '',
    wx: '',
    id: undefined,
    email: '',
    sign: '',
    roles: [],
    permissions: [],
    chartsData: []
  },

  mutations: {
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_PHONENUMBER:(state,phonenumber) => {
      state.phonenumber = phonenumber
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_CHARTS_DATA: (state, chartsData) => {
      state.chartsData = chartsData
    },
    SET_HEAD_CLASS: (state, headClass) => {
      state.headClass = headClass
    },
    SET_TYPE: (state, type) => {
      state.type = type
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_WX: (state, wx) => {
      state.wx = wx
    },
    SET_QQ: (state, qq) => {
      state.qq = qq
    },
    SET_NICKNAME: (state, nickName) => {
      state.nickName = nickName
    },
    SET_TAG_NAME: (state, tagName) => {
      state.tagName = tagName
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    }
  },

  actions: {
    //修改用户状态
    chageStatus(data) {
      axios({
        method: 'put',
        url: '',
        data: data
      })
    },

    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.userName.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          charts().then((response) => {
            commit('SET_CHARTS_DATA', response)
          })
          const user = res.user
          //process.env.VUE_APP_BASE_API +
          const avatar = user.avatar === '' ? require('@/assets/images/profile.jpg') : user.avatar
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.name)
          commit('SET_PHONENUMBER',user.phonenumber)
          commit('SET_AVATAR', avatar)
          commit('SET_ID', user.id)
          commit('SET_QQ', user.qq)
          commit('SET_WX', user.wx)
          commit('SET_HEAD_CLASS', user.headClass);
          commit('SET_NICKNAME', user.nickName);
          commit('SET_TAG_NAME', user.tagName);
          commit('SET_EMAIL', user.email);
          commit('SET_TYPE', user.type == 2 ? '普通成员' : '核心成员');
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
